package com.ossean.projectmanager.hotwords;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.ossean.projectmanager.lasttabledao.OpenSourceProjectDao;
import com.ossean.projectmanager.lasttabledao.TagDao;
import com.ossean.projectmanager.lasttabledao.TaggingsDao;
import com.ossean.projectmanager.model.OpenSourceProject;
import com.ossean.projectmanager.model.Taggings;
import com.ossean.projectmanager.utils.DataHandler;

/**
 * 
 * @author zhanyun
 * 
 */
@Component("updateTags")
public class UpdateOspTags {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Resource
	private OpenSourceProjectDao ospDao;
	@Resource
	private TagDao tagDao;
	@Resource
	private TaggingsDao taggingsDao;
	
	private String taggingsTableName = "taggings";
	private String tagsTableName = "tags";

	/**
	 * 根据taggings更新项目标签字段tags和权重更高的标签字段tags_for_search
	 */
	public void updateOspTags() {
		logger.info("start updateOspTags ......");
		int start = 0;
		int prjId = 0;
		while (start < 2000000) {
			List<OpenSourceProject> ospList = ospDao.getProjectsByBatch(start,
					5000);
			for (OpenSourceProject osp : ospList) {
				prjId = osp.getId();
				List<Taggings> taggingsList = taggingsDao.getByOspId(prjId);
				String tempTag = "";
				String tempTagForSearch = "";
				for (Taggings taggings : taggingsList) {
					String tag = tagDao.getNameById(taggings.getTag_id());
					tempTag += "<" + tag + ">,";
					if(taggings.getDisagree_num() > 50){
						tempTagForSearch += "<" + tag + ">,";
					}
				}
				String curPrjTag = "";
				String curPrjTagForSearch = "";
				if (tempTag.length() > 0) {
					curPrjTag = tempTag.substring(0, tempTag.length() - 1);
				}
				if (tempTagForSearch.length() > 0) {
					curPrjTagForSearch = tempTagForSearch.substring(0, tempTagForSearch.length() - 1);
				}
				ospDao.updatePrjTags(prjId, curPrjTag, curPrjTagForSearch);
				logger.info("currentPrjId: " + prjId);
			}
			logger.info("last prj batch end, currentPrjId: " + prjId);
			start = prjId + 5000;
		}

	}
	
	public void restorePrjTaggings(){
		logger.info("start updateOspTags ......");
		int start = 0;
		int prjId = 0;
		while (start < 2000000) {
			List<OpenSourceProject> ospList = ospDao.getProjectsByBatch(start,
					5000);
			for (OpenSourceProject osp : ospList) {
				prjId = osp.getId();
				// 将tag和项目的关系存入表item_tag_relation 并分离tag
				String tags = osp.getTags();
				if (tags == null) {
					// 表示该项目没有标签
					continue;
				}
				List<String> tagList = DataHandler
						.tagsSegmentation(tags);
				for (String tag : tagList) {
					ospDao.insertTag(tagsTableName, tag);// ignore方式插入该项目的标签
					int tag_id = ospDao.selectTagIdByName(
							tagsTableName, tag);
					Taggings taggings = new Taggings();
					taggings.setTag_id(tag_id);
					taggings.setTaggable_id(prjId);
					taggings.setTaggable_type("OpenSourceProject");
					taggings.setContext("tags");
					taggings.setCreated_at(DataHandler.getNow());
					// 将Taggings对象存入数据库中
					try {
						ospDao.insertTaggings(taggingsTableName,
								taggings);
					} catch (Exception e) {
						// 在插入记录之前 relative_memos表中的记录已经被删除掉了
						logger.error("insert taggins error: " + e);
						System.exit(0);
					}
				}
				logger.info("currentPrjId: " + prjId);
			}
			logger.info("last prj batch end, currentPrjId: " + prjId);
			start = prjId + 5000;
		}
		
	}

}
